
def urlopen(url):
    """Uses urllib2 to connect and retrieve a url with gzip encoding
    supported.
    
    usage is simple "urlopen("http://your-url.info")" returns a file-like object
    
    will return a normal urllib2.urlopen() response if content isn't gzipped
    """
    import urllib2
    import StringIO
    import gzip
    request = urllib2.Request(url)
    request.add_header("Accept-Encoding", "gzip")
    conn = urllib2.urlopen(request)
    if (conn.info().get("Content-Encoding") == "gzip"):
        f = gzip.GzipFile(fileobj=StringIO.StringIO(conn.read()))
        conn.close()
        return f
    return conn